package com.example.designpattenexample.combination;

import java.util.List;

public class OrgsTree {

    public static void getOrgsTreeInfo(CurrentOrg organization){
        List<Organization> list = organization.getChildOrgs();
        list.stream().forEach(o->{
            if (o instanceof ChildOrg) {
                System.out.println(o.printInfo());
            }else {
                System.out.println(o.printInfo());
                getOrgsTreeInfo((CurrentOrg) o);
            }
        });
    }

    public static CurrentOrg buildOrgs(){
        CurrentOrg rootOrg = new CurrentOrg("root","0");
        CurrentOrg levelOne1 = new CurrentOrg("levelOne1","1");
        CurrentOrg levelOne2 = new CurrentOrg("levelOne2","2");
        ChildOrg childOrg1 = new ChildOrg("child3","3");
        ChildOrg childOrg2 = new ChildOrg("child4","4");
        rootOrg.addOrgs(levelOne1);
        rootOrg.addOrgs(levelOne2);
        levelOne1.addOrgs(childOrg1);
        levelOne2.addOrgs(childOrg2);
        return rootOrg;
    }

    public static void main(String[] args) {
        Organization orgsTree = buildOrgs();
        System.out.println(orgsTree.printInfo());
        getOrgsTreeInfo(buildOrgs());
    }
}
